<template>
  <!-- 新增部门的弹层 -->
  <el-dialog title="新增部门" :visible="showdialog" @close="closesheet">
    <el-form ref="formDept" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in employees"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" size="small" @click="addDepart">确定</el-button>
      <el-button size="small" @click="$emit('close-sheet', false)">
        取消
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/department'
export default {
  // 需要传入一个props变量来控制 显示或者隐藏
  props: {
    showdialog: {
      type: Boolean,
      default: false
    },
    currentDept: {
      type: Object,
      default: () => ({})
    },
    departdata: {
      type: Array,
      default: () => []
    }
  },
  data () {
    const validateCode = (rules, value, callback) => {
      let flag
      if (this.form.id) {
        flag = this.departdata.some(item => item.code === this.form.code && item.id !== this.form.id)
      } else {
        flag = this.departdata.some(item => item.id === this.form.id)
      }
      if (flag) {
        callback(new Error('code重复'))
      } else {
        callback()
      }
    }
    return {
      employees: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validateCode }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.getEmployees()
  },
  methods: {
    async getDepart (id) {
      try {
        const res = await getDepartDetail(id)
        this.form = res
      } catch (error) {
        console.log(error)
      }
    },
    async addDepart () {
      try {
        await this.$refs.formDept.validate()
        if (!this.form.id) {
          const res = await addDepartments({ ...this.form, pid: this.currentDept.id || '' })
          console.log(res)
          this.$message.success('添加成功')
        } else {
          const res = await updateDepartments(this.form)
          this.$message.success('修改成功')
          console.log(res)
        }

        this.$emit('close-sheet', false)
        this.$emit('get-list')
      } catch (error) {
        console.log(error)
      }
    },
    async closesheet () {
      await this.$refs.formDept.resetFields()
      this.$emit('close-sheet', false)
    },
    async getEmployees () {
      const res = await getEmployeeSimple()
      this.employees = res
    }
  }
}
</script>
